//
//  DTabBarViewController.m
//  DFrame
//
//  Created by DaiSuke on 16/8/22.
//  Copyright © 2016年 DaiSuke. All rights reserved.
//

#import "DTabBarViewController.h"
#import "DNavigationViewController.h"

#import "DTabBar.h"

#import "DHomeViewController.h"
#import "DMessagesViewController.h"
#import "DDiscoverViewController.h"
#import "DMeViewController.h"


@interface DTabBarViewController ()<DTabBarDelegate>

@property (nonatomic, weak) DTabBar *customerTabBar;

// 首页
@property (nonatomic, strong) UIViewController *home;

// 消息
@property (nonatomic, strong) UIViewController *message;

// 广场
@property (nonatomic, strong) UIViewController *discover;

// 我
@property (nonatomic, strong) UIViewController *mime;

@end

@implementation DTabBarViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    // 删除系统自动生成的UITabBarButton
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 初始化TabBar
    [self setupTabBar];
    
    // 初始化所有子控制器
    [self setupAllChildViewController];
}

- (void)setupTabBar{
    DTabBar *customerTabBar = [[DTabBar alloc] init];
    
    // 成为代理
    customerTabBar.delagate = self;
    
    customerTabBar.frame = self.tabBar.bounds;
    [self.tabBar addSubview:customerTabBar];
    self.customerTabBar = customerTabBar;
}


- (void)setupAllChildViewController{
    
    // 首页
    DHomeViewController *homeVC = [[DHomeViewController alloc] init];
    [self setupChildViewController:homeVC title: @"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"];
    self.home = homeVC;
    
    // 消息
    DMessagesViewController *message = [[DMessagesViewController alloc] init];
    [self setupChildViewController:message title:@"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected"];
    self.message = message;

    // 广场
    DDiscoverViewController *discover = [[DDiscoverViewController alloc] init];
    [self setupChildViewController:discover title:@"广场" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"];
    self.discover = discover;
    
    // 我
    DMeViewController *mime = [[DMeViewController alloc] init];
    [self setupChildViewController:mime title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"];
    self.mime = mime;
    
}

/**
 初始化一个子控制器
 */
- (void)setupChildViewController:(UIViewController *)child title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{
    
    // 设置控制器的属性
    child.title = title;
    child.tabBarItem.image = [UIImage imageNamed:imageName];
    
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    child.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 包装一个导航控制器
    DNavigationViewController *childNav = [[DNavigationViewController alloc] initWithRootViewController:child];
    [self addChildViewController:childNav];
    
    // 自定义导航栏
    [self.customerTabBar addTabBarButtonWithItem:child.tabBarItem];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -DTabBarDelegate方法
- (void)tabBar:(DTabBar *)tabBar didSelectedButtonFrom:(int)from to:(int)to{
    
    self.selectedIndex = to;
}

@end
